package com.hq.fxh.CustomerView.PopupWindow;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.PopupWindow;

import com.hq.fxh.R;
import com.hq.fxh.Utils.LogUtils;
import com.hq.fxh.Utils.Tool;

import java.util.Timer;
import java.util.TimerTask;


/**
 * Created by Administrator on 2017/2/16.
 */
public class PopupForComment extends PopupWindow implements View.OnClickListener {


    protected View rootView;
    protected EditText popupcommentEditContent;
    protected Button popupcomentSent;
    private Activity context;
    private View popView;
    CommentCallBack commentCallBack;

    public PopupForComment(Activity context, CommentCallBack commentCallBack) {
        super(context);
        this.context = context;
        this.commentCallBack = commentCallBack;
        popView = LayoutInflater.from(context).inflate(R.layout.popup_write_comment, null);
        initView(popView);
        initData();

    }


    public void openKeyboard() {

        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);

            }
        }, 300);
    }

    public void initData() {

        //设置PopupWindow的View
        this.setContentView(popView);
        //设置PopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置PopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        //设置PopupWindow弹出窗体可点击
        this.setFocusable(true);
        this.setAnimationStyle(R.style.dialog_anim_style);
        this.setOutsideTouchable(true);
        //设置SelectPicPopupWindow弹出窗体动画效果
        this.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
        this.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xffffffff);
    /*    openKeyboard();*/

        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
       this.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                Tool.setAlpha(context, 1f);
                popupcommentEditContent.setText(" ");

            }
        });


        popupcommentEditContent.setFocusable(true);
        popupcommentEditContent.setFocusableInTouchMode(true);
        popupcommentEditContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {


                if (!Tool.editIsEmpty(popupcommentEditContent)) {
                    popupcomentSent.setBackgroundResource(R.drawable.style_circle_redbtn);
                    popupcomentSent.setClickable(true);
                    popupcomentSent.setTextColor(Color.WHITE);


                } else {
                    popupcomentSent.setBackgroundResource(R.drawable.style_corner_greybtn);
                    popupcomentSent.setTextColor(Color.BLACK);
                    popupcomentSent.setClickable(false);
                }
            }
        });
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.popupcoment_sent) {
            String text = popupcommentEditContent.getText().toString();
            Log.e("text", text);
            boolean empty = TextUtils.isEmpty(popupcommentEditContent.getText());
            if (!empty) {
                commentCallBack.call(text);

                LogUtils.e("评论为" + text);
            }
            PopupForComment.this.dismiss();

        }
    }

    private void initView(View rootView) {
        popupcommentEditContent = (EditText) rootView.findViewById(R.id.popupcomment_edit_content);
        popupcomentSent = (Button) rootView.findViewById(R.id.popupcoment_sent);
        popupcomentSent.setOnClickListener(PopupForComment.this);
    }

    public interface CommentCallBack {
        void call(String comment);
    }
}
